<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确定</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  getBrandList,
  getSeriesList,
  getModelList,
  getPackageList,
  getColorsList
 } from '@/api/potenCus/basedata/searchTestDriveCars';
import { dictionary } from '@/utils/dictMixin';
import AjaxButton from '@/components/AjaxButton';

export default {
  name: 'ChannelModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    // this.init();
    this.getDictBrandList()
  },
  methods: {
    init() {

    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'modeBrandCode',
          placeholder: '请选择',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [{ required: true, message: '请选择品牌', trigger: 'blur' }],
          change: val => {
            this.changeBrandSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'modeSeriesCode',
          placeholder: '请选择',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [{ required: true, message: '请选择车系', trigger: 'blur' }],
          change: val => {
            this.changeSeriesSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请选择',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [{ required: true, message: '请选择车型', trigger: 'blur' }],
          change: val => {
            this.changeModelSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'modeConfigCode',
          placeholder: '请选择',
          disabled: this.type === 'edit',
          itemList: [],
          change: val => {
            this.changeColorSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '颜色',
          fieldName: 'modeColorCode',
          placeholder: '请选择',
          itemList: [],
        },

      ];
    },
    formChangeHandle(val) {
      // console.log("val",val)
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null){
        console.log("意向保存",this.formList)
        // if(){
          let params={
            modeBrandCode: this.formList[0].initialValue,
            // isMainIntent: 10041001,
            modeSeriesCode: this.formList[1].initialValue,
            modelCode: this.formList[2].initialValue,
            modeConfigCode: this.formList[3].initialValue,
            modeColorCode: this.formList[4].initialValue,
          }
        // }
        this.$emit('YXHandle', params);
        this.closeDialog('refresh');
      } else {
        this.$message.info('请填写必填项!');
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async getDictBrandList() {
      const res = await getBrandList();
      // console.log("品牌YX",res)
      if (res.resultCode === 200) {
        this.formList[0].itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
      }
    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.formList[1].initialValue = '';
        this.formList[1].itemList = [];
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
      } else {
        const res = await getSeriesList(brandsid);
        if (res.resultCode === 200) {
          // console.log("车系YX",res)
          this.formList[1].itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
        if (this.formList[1].initialValue ) {
          this.formList[1].initialValue = '';
          this.formList[2].initialValue = '';
          this.formList[3].initialValue = '';
          this.formList[4].initialValue = '';
        }
      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.formList[0].initialValue;
      if (!brandsid || !seriessid) {
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
      } else {
        const res = await getModelList(brandsid,seriessid);
        if (res.resultCode === 200) {
          // console.log("车型YX",res)
          this.formList[2].itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
        if (this.formList[2].initialValue ) {
          this.formList[2].initialValue = '';
          this.formList[3].initialValue = '';
          this.formList[4].initialValue = '';
        }
      }
    },
    async changeModelSelect(modelId) {
      const brandsid = this.formList[0].initialValue;
      const seriessid = this.formList[1].initialValue;
      if (!brandsid || !seriessid || !modelId) {
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
      } else {
        const res = await getPackageList(modelId);
        if (res.resultCode === 200) {
          // console.log("配置YX",res)
          this.formList[3].itemList = res.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
        if (this.formList[3].initialValue ) {
          this.formList[3].initialValue = '';
          this.formList[4].initialValue = '';
        }
      }
    },
    async changeColorSelect(packageId) {
      const brandsid = this.formList[0].initialValue;
      const seriessid = this.formList[1].initialValue;
      const modelId = this.formList[2].initialValue;
      if (!brandsid || !seriessid || !modelId || !packageId  ) {
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
      } else {
        const res = await getColorsList(packageId);
        if (res.resultCode === 200) {
          // console.log("颜色YX",res)
          this.formList[4].itemList = res.data.map(x => ({ value: x.colorId, text: x.colorName }));
        }
        if (this.formList[4].initialValue ) {
          this.formList[4].initialValue = '';
        }
      }
    },
  }
};
</script>

<style lang="less" scoped>
</style>
